﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;
using msBsc.AppLogic.DataAlc.cls;
using msBsc.Cls;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizMessageSystem : CustBizObject
    {
        private CDmDjkMessageSystem m_CDmDjkMessageSystem = new CDmDjkMessageSystem();
        private CDmDjkMessageSystem oDmDjkMessageSystem
        {
            get { return m_CDmDjkMessageSystem; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizMessageSystem()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkMessageSystem.DJK_MESSAGE_SYSTEM);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["MESSAGE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加系统消息
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="userid">用户iD</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-6-18
        public string AddMessage(string title, string message, string userid)
        {
            if (string.IsNullOrEmpty(message))
            {
                return "请输入消息信息";
            }
            if (string.IsNullOrEmpty(userid))
            {
                return "当前用户不存在";
            }

            DataSet ds = new CDmDjkMessageSystem();
            DataRow dr = ds.Tables[0].NewRow();

            dr["MESSAGE_ID"] = this.DataObj.NewID();
            dr["MESSAGE_TITLE"] = title;
            dr["MESSAGE_CONTENT"] = message;
            dr["ACCEPT_PERSON"] = userid;
            dr["SENDR_TIME"] = this.dtCurrDateTime;
            dr["ISREAD"] = 0;

            ds.Tables[0].Rows.Add(dr);
            this.Update(ds);
            return "OK";
        }

        /// <summary>
        /// 修改阅读状态
        /// </summary>
        /// <param name="msid">消息ID</param>
        /// <param name="status">消息状态 默认修改为已读</param>
        /// <returns>string</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-6-18
        public string UpdateStatus(string msid, int status = 1)
        {
            if (string.IsNullOrEmpty(msid))
            {
                return "该消息不存在";
            }

            DataSet dsMs = this.GetMsgById(msid);
            if (dsMs == null)
            {
                return "该消息不存在";
            }

            dsMs.Tables[0].Rows[0]["ISREAD"] = status;

            this.Update(dsMs);
            return "OK";
        }

        /// <summary>
        /// 修改阅读状态
        /// </summary>
        /// <param name="msid">要修改的消息</param>
        /// <param name="status">消息状态 默认修改为已读</param>
        /// <returns>string</returns>
        /// Author:任延华
        /// Creation date:2015-7-15
        public string UpdateStatus(DataSet dsMs, int status = 1)
        {
            if (dsMs == null)
            {
                return "该消息不存在";
            }

            dsMs.Tables[0].Rows[0]["ISREAD"] = status;

            this.Update(dsMs);
            return "OK";
        }

        /// <summary>
        /// 根据ID获取
        /// </summary>
        /// <param name="msgid"></param>
        /// <returns>DataSet</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-6-18
        private DataSet GetMsgById(string msgid)
        {
            if (string.IsNullOrEmpty(msgid))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("msid", ParameterDirection.Input, EDbType.String, 36, msgid));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT * FROM [dbo].[DJK_MESSAGE_SYSTEM] WHERE MESSAGE_ID=@msid  ORDER BY [SENDR_TIME] DESC", parmCollection);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        /// <summary>
        /// 跟 用户id 获取消息
        /// </summary>
        /// <param name="userid">接受人</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-6-18
        public DataTable GetMsgList(string userid, int status, int pageSize, int pageIndex)
        {
            if (string.IsNullOrEmpty(userid))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();

            parmCollection.Add(new CDbParm("pagesize", ParameterDirection.Input, EDbType.Integer, 10, pageSize));
            parmCollection.Add(new CDbParm("pageindex", ParameterDirection.Input, EDbType.Integer, 10, pageIndex));
            parmCollection.Add(new CDbParm("userid", ParameterDirection.Input, EDbType.String, 36, userid));
            parmCollection.Add(new CDbParm("status", ParameterDirection.Input, EDbType.Integer, 10, status));

            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_MESSAGE_LIST_VIEW", parmCollection);

            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }

            return ds.Tables[0];
        }


        /// <summary>
        /// 根据消息id和接收人id获取消息信息
        /// </summary>
        /// <param name="msgId">消息id</param>
        /// <param name="djackId">接收人id</param>
        /// <returns></returns>
        /// Author:任延华
        /// Creation date:2015-6-18
        public DataSet GetMsgInfo(string msgId, string djackId)
        {
            if (string.IsNullOrEmpty(msgId) || string.IsNullOrEmpty(djackId))
            {
                return null; ;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("msgId", ParameterDirection.Input, EDbType.String, 36, msgId));
            parmCollection.Add(new CDbParm("djackId", ParameterDirection.Input, EDbType.String, 36, djackId));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT * FROM [dbo].[DJK_MESSAGE_SYSTEM] WHERE MESSAGE_ID=@msgId  and ACCEPT_PERSON=@djackId", parmCollection);
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        /// <summary>
        /// 根据接收人id获取未读消息列表
        /// </summary>
        /// <param name="djackId">接收人id</param>
        /// <returns></returns>
        /// Author:任延华
        /// Creation date:2015-6-18
        public DataSet GetUnMsgList(string djackId)
        {
            if (string.IsNullOrEmpty(djackId))
            {
                return null; ;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("djackId", ParameterDirection.Input, EDbType.String, 36, djackId));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT * FROM [dbo].[DJK_MESSAGE_SYSTEM] WHERE ISREAD=0  and ACCEPT_PERSON=@djackId", parmCollection);
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }
    }
}
